requireJS(['constant', 'site', 'handlebars', 'public', 'uploadImage', 'static/js/page/common/common.js'], function (Constant, site, Handlebars, Public, UploadImage, Common) {
    Public.initProductSearch();
    Public.initCompany();
    Constant.initHandlebars(Handlebars);
    UploadImage.init();

    var url = {
        selectRepanymentWayList: "manager/selectRepanymentWayList.do",
        selectRenewalRead: "manager/selectRenewalRead.do",
        selectRenewal: "manager/selectRenewal.do",
        submitRenewal: "manager/submitRenewal.do",
        selectPaymentSort: "manager/selectPaymentSort.do",
        submitReceivablesInit: "manager/submitReceivablesInit.do",
        submitReceivables: "manager/submitReceivables.do",
        selectLoanMethodList: "manager/selectLoanMethodList.do",
        selectPageLogList: "manager/selectPageLogList.do",  //回访记录
        DailyInterestRate: "manager/DailyInterestRate.do",
        checkVisit: "manager/checkVisit.do",
        checkDiscuss: "manager/checkDiscuss.do",
        deleteDiscuss: "manager/deleteDiscuss.do",
        deliverDiscuss: "manager/deliverDiscuss.do",
        submitVisit: "manager/submitVisit.do",
        selectVisitType: "manager/selectVisitType.do",
        selectOrderVisitListAll: 'manager/selectOrderVisitListAll.do',
        transformation: 'manager/transformation.do'
    }

    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('#loan_way').append(html);
            }
        }
    })

    var repaymentWay = {};
    var numed;
    var flun_type;
    var is_month_repayment_money;
    var is_comprehensive;
    var periods;
    var late_days;
    var is_penalty;
    var is_late_fee;
    var admin_id;
    var at_name;
    var plan_payment_money;
    var repayment_way;
    var loan_type;
    var loandays;
    var apply_money;
    var interest;

    site.send({
        url: url.selectRepanymentWayList,
        callback: function (data) {
            data = JSON.parse(data)
            var list = data.content
            if (data.type == '1') {
                for (var i = 0; i < list.length; i++) {
                    var v = list[i];
                    repaymentWay[v.id] = v;
                }
            }
        }
    })

    //日期控件初始化
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        pickerPosition: 'bottom',
    })

    var order_id;
    var visit_id;

    var visitData = {
        pageNum: 1
    } //回访列表所需字段

    var frontData = {};//回访列表
    var submitVisitData = {};//提交回访
    var remindData = {};//提醒客户
    var remindSalesData = {};
    // 初始化
    var init = function () {
        site.send({
            url: url.selectOrderVisitListAll,//回访管理
            data: frontData,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == '1') {
                    $("#total").text(list.content.total);
                    var source = $("#orderVisit_content_tpl").html();
                    var template = Handlebars.compile(source);

                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: list,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }

                    var html = template(obj);
                    $("#orderVisit_content").html(html);
                    // 分页
                    $('.orderList-page-container').createPager({
                        current: frontData.pageNum,
                        total: list.content.total,
                        pageSize: list.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            frontData.pageNum = current;
                            init();
                        }
                    })

                    Public.record('t_order_public');
                } else {
                    Constant.fn.Message('F', list.msg);
                }

                onBindFun();
            }
        })


        //头部日期
        site.send({
            url: "manager/selectApplyTermList.do",
            async: false,
            data: {},
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == '1') {
                    var template2 = Handlebars.compile($("#contract_head_date_tpl").html())
                    var html2 = template2(list)
                    $("#contract_head_date").html(html2)
                } else {
                    Constant.fn.Message('F', list.msg);
                }
            }
        })
    }

    init();

    function queryRecord(frontData) {
        site.send({
            url: url.checkVisit,
            data: frontData,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#revisit_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var list = data.content;
                    var html = template(list);
                    $('#revisit_list').append(html);

                    $('.showMore').on('click', function () {
                        frontData.pageNum = $(this).data('pagenum') + 1;
                        queryRecord(frontData)
                        $(this).remove();
                    })

                    /*$('.comment').on('click',function(){
                        visit_id = $(this).data('id')
                        site.send({
                            url: url.checkDiscuss,
                            data: {
                                visit_id: visit_id
                            },
                            callback: function (data) {
                                data = JSON.parse(data);
                                if (data.type == 1) {
                                    var source = $("#comment_list_tpl").html();
                                    var template = Handlebars.compile(source);
                                    var list = data.content;
                                    var html = template(list);
                                    $('#comment_list').append(html)

                                    $(".commentTotal").html(data.content.totleSize);


                                    $('.deleteDiscuss').on('click',function(){
                                        var _this = this;
                                        var discuss_id = $(_this).data('id');
                                        site.send({
                                            url: url.deleteDiscuss,
                                            data: {
                                                discuss_id: discuss_id
                                            },
                                            callback:function(data){
                                                data = JSON.parse(data);
                                                if(data.type == '1'){
                                                    Constant.fn.Message("T",data.msg);
                                                    $(_this).parents('.comment_list').remove();
                                                }else{
                                                    Constant.fn.Message("F",data.msg);
                                                }
                                            }
                                        })
                                    })


                                }else{
                                    Constant.fn.Message("F",data.msg);
                                }
                            }
                        })
                        $('.comment_winBox').show();
                    })*/


                    //分页
                    $('.revisit-page-container').createPager({
                        current: frontData.pageNum,
                        total: data.content.total,
                        pageSize: data.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            frontData.pageNum = current;
                            queryRecord(frontData);
                        }
                    })
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    }

    $('#deliverDiscuss').on('click', function () {
        //var discuss_id = $(this).data('id');
        site.send({
            url: url.deliverDiscuss,
            data: {
                visit_id: visit_id,
                discuss_remark: $(".commentContent").val()
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    Constant.fn.Message("T", data.msg);
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    })


    // on操作事件
    function onBindFun() {
        $(".visitSelect").on('change', function () {
                order_id = $(this).data('id');
                numed = $(this).data('numed');
                flun_type = $(this).data('fluntype');
                admin_id = $(this).data('adminid');
                repayment_way = $(this).data('repayment_way');
                at_name = $(this).data('at_name');
                loan_type = $(this).data('loan_type');
                loandays = $(this).data('at_id');
                apply_money = $(this).data('apply_money');
                interest = $(this).data('interest');
                //提交收款所需字段

                var obj = $(this).find("option:selected");
                if (obj.val() == 'checkOrder') {
                    pjaxGo('loanLater/loanLaterDetail.html?order_id=' + $(this).data('id') + "&product_id=" + $(this).data('productid'));
                }

                if (obj.val() == 'submitVisit') {
                    //提交回访回访类型渲染
                    Common.visit(order_id);
                    Common.init(1, order_id);
                }

                if (obj.val() == 'sumbitGathering') {
                    $(".visitSelect").val('-1');
                    site.send({
                        url: url.selectPaymentSort,
                        callback: function (data) {
                            data = JSON.parse(data);
                            if (data.type == '1') {
                                var sortList = data.content;
                                var sortSource = $("#paymentSort_list_tpl").html();
                                var sortTemplate = Handlebars.compile(sortSource);
                                var sorthtml = sortTemplate(sortList);
                                var ps_id = data.content[0].id;

                                //日期控件初始化
                                $(".datepicker").datepicker({
                                    format: 'yyyy-mm-dd',
                                    autoclose: true,
                                    pickerPosition: 'bottom',
                                })

                                site.send({
                                    url: url.submitReceivablesInit,
                                    data: {
                                        order_id: order_id
                                    },
                                    callback: function (data) {
                                        data = JSON.parse(data);
                                        if (data.type == '1') {
                                            $("#putLoanRemarks").val("");
                                            $('.putLoan_winBox').show();
                                            var list = data.content;
                                            var funPlan = data.content.funPlanlList[0]
                                            if (funPlan) {
                                                funPlan.now_money = funPlan.is_month_repayment_money + funPlan.is_moth_repayment_rate + funPlan.is_comprehensive
                                            }

                                            var num;
                                            var changeEle = {
                                                penalty: true,
                                                late_fee: true,
                                                huaizhang: true,
                                                qishu: true,
                                                benjin: true
                                            };
                                            var depositReturn = -1;
                                            var depositTF;

                                            list.ps_id = ps_id;
                                            list.loan_type = loan_type;
                                            var sum = 0;
                                            var sum_name = at_name.split('个')[0];
                                            var depositOne, depositTwo, depositThere, depositFour;
                                            $.each(list.depositMap, function (index, val) {
                                                sum += val.deposit
                                                if (val.type == 1) {
                                                    depositOne = val.deposit
                                                }
                                                if (val.type == 2) {
                                                    depositTwo = val.deposit
                                                }
                                                if (val.type == 3) {
                                                    depositThere = val.deposit
                                                }
                                                if (val.type == 4) {
                                                    depositFour = val.deposit
                                                }
                                            })
                                            list.sum = sum;
                                            list.depositOne = depositOne;
                                            list.sum_name = sum_name;
                                            if (list.funPlanlList.length) {
                                                list.period_num = list.funPlanlList[0].periods.slice(1, -1);
                                            }

                                            if (ps_id == 5 || (ps_id == 1 && num == list.period_num)) {
                                                changeEle.money = data.content.maps.sumMoney
                                            } else {
                                                changeEle.money = funPlan.now_money
                                            }

                                            if (depositReturn == 1) {
                                                plan_payment_money = changeEle.money - sum
                                            } else if (depositReturn == -1) {
                                                plan_payment_money = changeEle.money
                                            }

                                            list.funPlan = funPlan
                                            list.plan_payment_money = plan_payment_money;

                                            var source = $("#putLoan_list_tpl").html();
                                            var template = Handlebars.compile(source);
                                            var html = template(list);
                                            $("#putLoan_list").html(html);

                                            /*var source2 = $("#special_putLoan_list_tpl").html();
                                            var template2 = Handlebars.compile(source2);
                                            var html2 = template2(list);*/

                                            /*if (repayment_way != '4') {
                                                $("#putLoan_list").html(html);
                                            } else {
                                                $("#putLoan_list").html(html2);
                                                $("#qishu").html("本金（" + Constant.tplHelper.AMOUNT_FORMAT(list.applyMoney) + "）+【本金（" + Constant.tplHelper.AMOUNT_FORMAT(list.applyMoney) + "）*利息（" + list.interest / 100 + "）*天数（" + list.days + "）】");
                                            }

                                            if (repayment_way != '4') {
                                                $("#paymentSort_list").html(sorthtml);
                                            }*/
                                            /*else{
                                                                                        $("#paymentSort_list").parents('td').html('按天计息');
                                                                                    }*/
                                            //借款期限输入框填完后计算利息
                                            $("#days").on('blur', function () {
                                                if (!$(this).val() || parseInt($(this).val()) < parseInt(loandays)) {
                                                    Constant.fn.Message("I", "请输入大于借款日期的日期");
                                                    return;
                                                }
                                                ;
                                                $("#payedDays").text(loandays);
                                                $("#paying").show();
                                                $("#payed").show();
                                                $("#payingDays").text(parseInt($(this).val()) - parseInt(loandays));
                                                $("#daysApplyMoney").text(Constant.tplHelper.AMOUNT_FORMAT(apply_money));
                                                $("#daysApplyInterest").text(Constant.tplHelper.AMOUNT_FORMAT((parseInt($(this).val()) - parseInt(loandays)) * apply_money * interest / 100));
                                            })

                                            $("#paymentSort_list").html(sorthtml);

                                            $("#paymentSort_list").on('change', function () {
                                                if ($(this).val() == '1') {
                                                    $("#deposit,#guarantee,#penalty,#late_fee_need,#beijin,#collectByDaysDetail,#collectByDays,.isBenJin").hide();
                                                    $(".comprehensive_fee").hide();
                                                    $("#transfer,#transerWay,#gathering,#come,#notbeijin,#qishu,.checkAccout,#thisTerm,#thisCollect,.collectDetail").show();
                                                    $("#zonghe").hide();
                                                    plan_payment_money = changeEle.money
                                                    $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                }
                                                else if ($(this).val() == '2') {
                                                    $("#deposit,#guarantee,#penalty,#late_fee_need,#transfer,#transerWay,#beijin,#gathering,#come,#collectByDaysDetail,#collectByDays,.isBenJin").hide();
                                                    $(".comprehensive_fee").hide();
                                                    $("#notbeijin,#qishu,.checkAccout,#thisTerm,#thisCollect,.collectDetail").show();
                                                    $("#zonghe").hide();
                                                    plan_payment_money = changeEle.money
                                                    $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                }
                                                else if ($(this).val() == '4') {
                                                    $("#deposit,#guarantee,#penalty,#beijin,#collectByDaysDetail,#collectByDays,.isBenJin").hide();
                                                    $(".comprehensive_fee").hide();
                                                    $("#late_fee_need,#transfer,#transerWay,#gathering,#come,#notbeijin,#qishu,.checkAccout,#thisTerm,#thisCollect,.collectDetail").show();
                                                    $("#zonghe").hide();
                                                    plan_payment_money = changeEle.money
                                                    $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                }
                                                else if ($(this).val() == '5') {
                                                    $("#deposit,#guarantee,#penalty,#late_fee_need,#transfer,#transerWay,#come,#notbeijin,#beijin,.checkAccout,#thisTerm,#thisCollect,.collectDetail,.isBenJin").show();
                                                    $("#notbeijin,#qishu,#collectByDaysDetail,#collectByDays").hide();
                                                    $(".comprehensive_fee").hide();
                                                    $("#zonghe").hide();
                                                    changeEle._money = data.content.maps.sumMoney
                                                    if (depositReturn == 1) {
                                                        plan_payment_money = changeEle._money - sum
                                                    } else if (depositReturn == -1) {
                                                        plan_payment_money = changeEle._money
                                                    }
                                                    $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                }
                                                else if ($(this).val() == '6') {

                                                }
                                                else if ($(this).val() == '7') {
                                                    $("#deposit,#guarantee,#penalty,#late_fee_need,#notbeijin,#beijin,#collectByDaysDetail,#collectByDays,.isBenJin").hide();
                                                    $("#transfer,#transerWay,#gathering,#come,#qishu,.checkAccout,#thisTerm,#thisCollect,.collectDetail").show();
                                                    $(".comprehensive_fee").show();
                                                    if (loan_type == '1') {
                                                        if (list.funMapLoan) {
                                                            plan_payment_money = list.funMapLoan.is_moth_repayment_rate + list.funMapLoan.is_month_repayment_money + list.funMapLoan.is_comprehensive
                                                            $(".periods").html(list.funMapLoan.periods);
                                                            $(".plan_time").html(list.funMapLoan.repayment_time);
                                                        } else {
                                                            plan_payment_money = 0;
                                                        }
                                                    } else {
                                                        plan_payment_money = list.funMap.is_moth_repayment_rate + list.funMap.is_month_repayment_money + list.funMap.is_comprehensive
                                                    }
                                                    $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                    $("#zonghe").show();
                                                }
                                                else if ($(this).val() == '8') {
                                                    $("#thisTerm,#thisCollect,.collectDetail,#collectByDaysDetail,#collectByDays,.isBenJin").hide();
                                                }
                                                else if ($(this).val() == '9') {
                                                    $("#deposit,#guarantee,#penalty,#late_fee_need,#beijin,#collectByDaysDetail,#collectByDays").hide();
                                                    $(".comprehensive_fee").hide();
                                                    $("#thisCollect,#zonghe,#notbeijin,#beijin,#collectByDays").hide();
                                                    $("#collectByDays,#collectByDaysDetail,#come,.isBenJin").show();
                                                }
                                            })

                                            $(".datepicker").datepicker();

                                            //提交收款初始化处理，提交收款业务逻辑比较复杂
                                            $("input[name='isLate']").on("click", function () {
                                                if ($(this).val() == '1') {
                                                    $("#late_fee").show();
                                                } else {
                                                    $("#late_fee").hide();
                                                    $("#late_fee_val").val(0);
                                                }
                                            })

                                            $("input[name='isBenJin']").on("click", function () {
                                                if ($(this).val() == '1') {
                                                    $("#benjin").hide();
                                                } else {
                                                    $("#benjin").show();
                                                }
                                            })

                                            $("#depositReturn").on('click', function () {
                                                //先去除千分符
                                                if ($(this).is(":checked")) {
                                                    depositReturn = 1;
                                                    plan_payment_money = (changeEle._money || changeEle.money) - sum
                                                    if (plan_payment_money < 0) {
                                                        plan_payment_money = changeEle._money
                                                        $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                        depositReturn = -1;
                                                        depositTF = false;
                                                        Constant.fn.Message('I', '不能押金转本金');
                                                        return;
                                                    }
                                                } else {
                                                    depositReturn = -1;
                                                    depositTF = false;
                                                    plan_payment_money = (changeEle._money || changeEle.money);
                                                    $("#plan_payment_money").html('￥' + Constant.tplHelper.AMOUNT_FORMAT(plan_payment_money));
                                                }
                                            })

                                            $("#Depositforbreach").on('click', function () {
                                                if ($(this).is(":checked")) {
                                                    //depositforbreach = 1
                                                    //depositTB = true
                                                    //penalty =sum || 123
                                                } else {
                                                    //depositforbreach = -1
                                                    //depositTB = false
                                                }
                                            })
                                        }
                                        else {
                                            Constant.fn.Message("F", data.msg)
                                        }
                                    }
                                })
                            }
                            else {
                                Constant.fn.Message("F", data.msg)
                            }
                        }
                    })
                }
                if (obj.val() == 'remindPerson') {
                    remindData.order_id = order_id;
                    $("#remindRemarks").val("");
                    $(".remindWinbox").show();
                    $(".visitSelect").val('-1');
                }
                if (obj.val() == 'remindSalesMan') {
                    remindSalesData.adminId = admin_id;
                    $("#remindSalesRemarks").val("");
                    $(".remindSalesWinbox").show();
                    $(".visitSelect").val('-1');
                }
                if (obj.val() == 'loanRecord') {
                    site.send({
                        url: 'manager/collectionRecord.do',//提交回访
                        //async:false,
                        data: {
                            order_id: order_id
                        },
                        callback: function (data) {
                            var list = JSON.parse(data);
                            if (list.type == '1') {
                                var list = JSON.parse(data);
                                var template = Handlebars.compile($("#loanRecord_content_tpl").html());
                                var html = template(list.content);
                                $("#loanRecord").html(html);
                                $(".payMentWinbox").show();
                            } else {
                                Constant.fn.Message('F', list.msg);
                            }
                        }
                    })
                    $(".visitSelect").val('-1');
                }
                if (obj.val() == 'selectRenewalRead') {
                    //初始化弹窗
                    $('#preview_content').html('');

                    $('.renewal_winBox').toggle();
                    site.send({
                        url: url.selectRenewalRead,
                        data: {
                            oid: order_id
                        },
                        callback: function (data) {
                            data = JSON.parse(data);
                            if (data.type == '1') {
                                var source = $("#renewal_content_tpl").html();
                                var template = Handlebars.compile(source);
                                data.content.flun_type = flun_type;
                                var html = template(data.content);
                                $("#renewal_content").html(html);

                                UploadImage.OSS_IMAGE_INIT();
                                //$("#numed").html(numed);
                                $("#repayment_way").html(repaymentWay[parseInt(data.content.repayment_way)].re_name);

                                $("#num").on('change', function () {
                                    if ($(this).val()) {
                                        $('#afterRenewal').html(parseInt($("#sumP").text()) + parseInt($(this).val()) + '期');
                                    }
                                    else {
                                        $('#afterRenewal').html('')
                                    }
                                })
                            }
                        }
                    })
                    $(".visitSelect").val('-1');
                }
                if (obj.val() == 'revisitRecord') {
                    $("#revisit_pop").toggle();
                    visitData.order_id = order_id
                    visitData.pageSize = 3;
                    visitData.type = 1;
                    $("#revisit_list").html("");
                    queryRecord(visitData);
                }
                if (obj.val() == 'addTerm') {
                    $('.addTerm_winBox').show();
                }
                $(".visitSelect").val('-1')
            }
        )
    }

    //提交收款确认按钮
    $("#surePutLoan").on('click', function () {
        var pmType = $("input[name='isCheck']:checked").val();
        var daytype = $("input[name='isBenJin']:checked").val();
        //var plan_payment_money = $("#plan_payment_money").data('plan_payment_money');
        var data = {
            oid: order_id,
            ps_id: $("#paymentSort_list").val(),
            plan_payment_money: plan_payment_money,
            //real_payment_money: $("#real_payment_money").val(),
            //receipt_number: $("#transferMan").val(),
            //transfer_mode: $("#trans").val(),
            //real_time: $(".real_time").val(),
            fund_plan_id: $("#fund_plan_id").val(),
            //penalty: $("#penalty_val").val() ? $("#penalty_val").val() : 0,
            //late_fee: $("#late_fee_val").val() ? $("#late_fee_val").val() : 0,
            //depositReturn:
            status: 1,
            late_days: late_days,
            r_penalty: is_penalty,
            r_late_fee: is_late_fee,
            is_month_repayment_money: is_month_repayment_money,
            remarks: $("#putLoanRemarks").val(),
        };
        if ($("#real_payment_money").parents('tr').css('display') != 'none') {
            data.real_payment_money = $("#real_payment_money").val()
        }

        if ($("#transferMan").parents('tr').css('display') != 'none') {
            data.receipt_number = $("#transferMan").val()
        }

        if ($("#trans").parents('tr').css('display') != 'none') {
            data.transfer_mode = $("#trans").val()
        }

        if ($(".real_time").parents('tr').css('display') != 'none') {
            data.real_time = $(".real_time").val()
        }

        $.each($(".real_time"), function (i, e) {
            if ($(e).parents('tr').css('display') != 'none' && $(e).val()) {
                data.real_time = $(e).val()
            }
        })


        if ($("#penalty_val").parents('tr').css('display') != 'none') {
            data.penalty = $("#penalty_val").val()
        }

        if ($("#late_fee_val").parents('tr').css('display') != 'none') {
            data.late_fee = $("#late_fee_val").val()
        }

        if ($("#depositReturn").parents('tr').css('display') != 'none' && $("#depositReturn").is(":checked")) {
            data.depositReturn = 1;
        }

        if ($("#Depositforbreach").parents('tr').css('display') != 'none' && $("#Depositforbreach").is(":checked")) {
            data.Depositforbreach = 2;
        }
        if (!pmType && $('.checkAccout').css('display') != 'none') {
            Constant.fn.Message("I", "请选择是否需要复核到账");
            return;
        }
        if (pmType) {
            data.pmtype = pmType;
        }
        if ($("#gathering").css('display') != 'none' && !$("#real_payment_money").val()) {
            Constant.fn.Message("I", "请填写实际收款");
            return;
        }
        if ($("#come").css('display') != 'none' && !$("#come").find(".real_time").val()) {
            Constant.fn.Message("I", "请填写到账日期");
            return;
        }
        if ($("#paymentSort_list").val() === '9' && !daytype) {
            Constant.fn.Message("I", "请选择是否收取本金");
            return;
        }
        if (daytype) {
            data.daytype = daytype;
        }
        if ($("#days").val()) {
            data.days = $("#days").val();
        }
        site.send({
            url: url.submitReceivables,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    Constant.fn.Message("T", data.msg);
                    $(".putLoan_winBox").hide();
                    init()
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            },
        })
    })



    //提交添加展期按钮
    $("#sureAddTerm").on('click', function () {
        var interest = $('#add_interest').val();
        var days = $("#addTerm_days").val();
        var realyMoney = interest * days * apply_money / 100;
        var real_time = $('.addTerm_winBox .real_time').val();
        if(!interest){
            Constant.fn.Message('I','请填写利率');
            return;
        }
        if(!days){
            Constant.fn.Message('I','请填写续期天数');
            return;
        }
        if(!real_time){
            Constant.fn.Message('I','请填写还款天数');
            return;
        }
        //var realyMoney =
        var data = {
            oid: order_id,
            interest: interest,
            days: days,
            realyMoney: realyMoney,
            real_time: real_time
        }
        site.send({
            url: url.transformation,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    Constant.fn.Message("T", data.msg);
                    $(".addTerm_winBox").hide();
                    init()
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            },
        })
    })


    //利率日期失焦
    $('#add_interest,#addTerm_days').on('blur',function(){
        var days = $("#addTerm_days").val();
        var interest = $("#add_interest").val();
        var realyMoney = days * interest * apply_money / 100;
        if(realyMoney){
            $("#interest_rate").html('利息：￥' + Constant.tplHelper.AMOUNT_FORMAT(realyMoney));
        }else{
            $("#interest_rate").html('利息：￥0');
        }
    })

    /*// 提交回访确认按钮
    $("#commitVisit").on('click', function () {
        // submitVisitData.url=
        submitVisitData.remarks = $("#visitWinboxtexTarea").val();
        site.send({
            url: 'manager/submitVisit.do',//提交回访
            //async:false,
            data: submitVisitData,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == '1') {
                    $(".visitWinbox").hide();
                    Constant.fn.Message('T', list.msg);
                    init();
                } else {
                    Constant.fn.Message('F', list.msg);
                }
            }
        })
    })*/

    $("#orderListSerach").on('click', function () {
        frontData = {};
        if ($("#d2Id").val() != '') {
            frontData.d2Id = $("#d2Id").val();
        }
        if ($("#start_time").val() != '') {
            frontData.start_time = $("#start_time").val();
        }
        if ($("#end_time").val() != '') {
            frontData.end_time = $("#end_time").val();
        }
        if ($("#time_type").val() != '') {
            frontData.time_type = $("#time_type").val();
        }
        if ($("#atId").val() != '') {
            frontData.atId = $("#atId").val();
        }
        if ($("#inputNames").val() != '' && $("#inputNames").val().trim()) {
            frontData.name = $("#inputNames").val();
        }
        if ($("#loan_way").val()) {
            frontData.loan_way = $("#loan_way").val();
        }
        if ($("#seleNames").val()) {
            frontData.type = $("#seleNames").val();
        }
        if ($("#product_choose_list").val()) {
            frontData.product_id = $("#product_choose_list").val()
        }
        if ($("#t_state").val()) {
            frontData.t_state = $("#t_state").val()
        }
        init();
    })

    // 提醒客户
    $("#remindBtn").on('click', function () {
        remindData.remarks = $("#remindRemarks").val()
        remindFun();
    })

    //提醒业务员
    $("#remindSalesBtn").on('click', function () {
        remindSalesData.remarks = $("#remindSalesRemarks").val()
        if (!$("#remindSalesRemarks").val()) {
            Constant.fn.Message("I", "请输入提醒信息")
            return;
        }
        remindSales();
    })

    function remindFun() {
        site.send({
            url: 'manager/remindCustomers.do',//提交回访
            //async:false,
            data: remindData,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == '1') {
                    $(".remindWinbox").hide();
                    Constant.fn.Message('T', list.msg);
                    init();
                } else {
                    Constant.fn.Message('F', list.msg);
                }
            }
        })
    }

    function remindSales() {
        site.send({
            url: 'manager/sendCollect.do',//提醒业务员
            //async:false,
            data: remindSalesData,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == '1') {
                    $(".remindSalesWinbox").hide();
                    Constant.fn.Message("T", list.msg);
                } else {
                    Constant.fn.Message('F', list.msg);
                }
            }
        })
    }


    $("#preview").on('click', function () {
        var data = {
            oid: order_id,
            sumNum: parseInt($("#sumP").text()) + parseInt($("#num").val()),
            flun_type: flun_type,
            update_type: $("input[name=update_type]:checked").val(),
            sumP: parseInt($("#sumP").text()),
            fulpanNum: numed,
        }

        var smallMoney = $("#smallMoney").val();
        var interest = $("#interest").val();

        if (smallMoney) {
            data.smallMoney = smallMoney;
        }

        if (interest) {
            data.r_interest = interest;
        }

        if (!$("#num").val()) {
            Constant.fn.Message("I", "请选择新增期数");
            return;
        }

        if (!data.update_type) {
            Constant.fn.Message("I", "请选择展期类型");
            return;
        }

        site.send({
            url: url.selectRenewal,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    /*$('.renewal_winBox').toggle();
                    location.reload();*/

                    var source = $("#preview_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $("#preview_content").html(html).show();

                }
                else {

                }
            }
        })
    })

    $("#sureRenewal").on('click', function () {
        var data = {
            oid: order_id,
            sumNum: parseInt($("#sumP").text()) + parseInt($("#num").val()),
            flun_type: flun_type,
            update_type: $("input[name=update_type]:checked").val(),
            sumP: parseInt($("#sumP").text()),
            fulpanNum: numed
        }


        var smallMoney = $("#smallMoney").val();
        var interest = $("#interest").val();

        if (smallMoney) {
            data.smallMoney = smallMoney;
        }

        if (interest) {
            data.r_interest = interest;
        }

        if (!$("#num").val()) {
            Constant.fn.Message("I", "请选择新增期数");
            return;
        }

        if (!data.update_type) {
            Constant.fn.Message("I", "请选择展期类型");
            return;
        }

        site.send({
            url: url.submitRenewal,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    $('.renewal_winBox').toggle();
                    Constant.fn.Message("T", data.msg)
                    init();

                    /*var source = $("#preview_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $("#preview_content").html(html).show();*/
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            }
        })
    })
})



